package tq.com.tqteacher.adapt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.tqteacher.R;
import tq.com.tqteacher.model.TQDirectoryModel;
import tq.com.tqteacher.model.TQFileInfo;

public class TQDirectoryDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<TQDirectoryModel.TQDirectoryItem> data = new ArrayList<>();
    static public int TYPE_TITLE = 0x01;

    public TQDirectoryDetailAdapter(Context pContext){
        mContext = pContext;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == TYPE_TITLE){
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_directory_detail_title_layout, pViewGroup, false);
            return new TitleViewHold(lView);
        }else{
            View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_grideview_item_layout, pViewGroup, false);
            return new FileViewHold(lView);
        }
    }

    @Override
    public int getItemCount() {
        return data.size() * 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position%2 == 0) return TYPE_TITLE;
        return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        int item = pI / 2;
        int all = getItemCount() / 2 - 1;

        TQDirectoryModel.TQDirectoryItem lItem = data.get(item);
        if(pI % 2 == 0 && pViewHolder instanceof TitleViewHold){
            ((TitleViewHold)pViewHolder).bind(lItem);
        }else if(pViewHolder instanceof FileViewHold){
            ((FileViewHold)pViewHolder).bindData(lItem, all == item);
        }
    }

    public void appendData(ArrayList<TQDirectoryModel.TQDirectoryItem> pList){
        data.addAll(pList);
        notifyDataSetChanged();
    }

    public void clear(){
        data.clear();
        notifyDataSetChanged();
    }


    public class TitleViewHold extends RecyclerView.ViewHolder {
        private TextView mTitle;
        public TitleViewHold(View pView){
            super(pView);
            mTitle = pView.findViewById(R.id.title);
        }
        public void bind(TQDirectoryModel.TQDirectoryItem pItem){
            if(pItem == null) return;
            mTitle.setText(pItem.getTitle());
        }
    }

    public class FileViewHold extends RecyclerView.ViewHolder{
        private GridView mGridView;
        private ArrayList<HashMap<String, String>> data = new ArrayList<>();
        private FileAdapter lSimpleAdapter;
        private TQDirectoryModel.TQDirectoryItem mDirectoryItem;
        private View line;

        public FileViewHold(View v){
            super(v);
            mGridView = v.findViewById(R.id.gridView);

            lSimpleAdapter = new FileAdapter();//new SimpleAdapter(mContext, data, R.layout.vh_file_layout, new String[]{"title"}, new int[]{R.id.title});

            mGridView.setAdapter(lSimpleAdapter);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(mDirectoryItem.getTQFileItems().size() > 0){
                        TQFileInfo lInfo = mDirectoryItem.getTQFileItems().get(position);

                        if(mFileClickListener != null) mFileClickListener.itemClick(lInfo);
                    }

                }
            });

            line = v.findViewById(R.id.line);
        }

        public void bindData(TQDirectoryModel.TQDirectoryItem pItem, boolean isLast){
            mDirectoryItem = pItem;
            if(mDirectoryItem == null){
                return;
            }

            line.setVisibility(isLast ? View.INVISIBLE : View.VISIBLE);
            lSimpleAdapter.notifyDataSetChanged();
        }

        class FileAdapter extends BaseAdapter{

            @Override
            public Object getItem(int position) {
                return mDirectoryItem.getTQFileItems().get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public int getCount() {
                if(mDirectoryItem == null){
                    return 0;
                }
                return mDirectoryItem.getTQFileItems().size();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View lView = convertView;
                if(lView == null){
                    lView = LayoutInflater.from(mContext).inflate(R.layout.vh_file_layout, parent, false);
                }

                TQFileInfo lInfo = mDirectoryItem.getTQFileItems().get(position);
                TextView lTextView = lView.findViewById(R.id.title);
                lTextView.setText(lInfo.getName());

                ImageView icon = lView.findViewById(R.id.icon);
                if(lInfo.isMp4()){
                    icon.setImageResource(R.drawable.icon_mp4);;
                }else{
                    icon.setImageResource(R.drawable.icon_pdf);
                }
                return lView;
            }
        }
    }

    public interface IFileClickListener{
        void itemClick(TQFileInfo url);
    }

    private IFileClickListener mFileClickListener;
    public void setFileClickListener(IFileClickListener pListener){
        mFileClickListener = pListener;
    }
}
